import axios from 'axios'

const BASE_OPTIONS = {
  baseURL: 'http://123.207.32.32:8000/api/h8',
  // 一般超时时间设置 20s~30s 之间
  timeout: 2000000
}

// 个人网络请求库axios >>> 封装
const instance = axios.create(BASE_OPTIONS);

// 请求拦截器
instance.interceptors.request.use(
  config => {
    return config;
  },
  err => {
    // ...
    return Promise.reject(err);
  }
)

// 响应拦截器
instance.interceptors.response.use(
  res => {
    // console.log("[响应拦截] 响应数据已获取", res);
    return res.data;
  },
  err => {
    // ...
    console.log("[异常] 响应失败 >>>", err);
    return Promise.reject(err);
  }
)

// 默认导出当前axios实例对象
export default instance;

// 通用请求方式封装 GET、POST、DELETE...
// GET
export const GET = (url, params) => {
  return instance.get(url, { params });
}

// POST
export const POST = (url, params) => {
  return instance.post(url, params);
}

// DELETE
export const DELETE = (url, params) => {
  return instance.delete(url, { params });
}

// PUT
export const PUT = (url, params) => {
  return instance.put(url, params);
}

// PATCH
export const PATCH = (url, params) => {
  return instance.patch(url, params);
}